<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog v-dialogDrag :title="dialogTitle" :visible.sync="dialogVisible" append-to-body width="45%">
      <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="100px">
        <el-form-item label="名字" prop="name">
          <el-input v-model.trim="formData.name" placeholder="请输入名字"/>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="formData.type" placeholder="请选择类型">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_OPERATE_GUIDE_TYPE)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="所属模块" prop="module">
          <el-select v-model="formData.module" placeholder="请选择所属模块">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_OPERATE_GUIDE_MODULE)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="图片地址">
          <ImageUpload v-model="formData.picUrl"/>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model='formData.sort' :min="0" :max="100" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as OperateGuideApi from '@/api/system/operateguide';
import ImageUpload from '@/components/ImageUpload';
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "OperateGuideForm",
  components: {
    ImageUpload,
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        name: undefined,
        type: undefined,
        module: undefined,
        picUrl: undefined,
        sort: undefined,
      },
      // 表单校验
      formRules: {},
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await OperateGuideApi.getOperateGuide(id);
          this.formData = res.data;
          this.title = "修改系统操作引导";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增系统操作引导";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await OperateGuideApi.updateOperateGuide(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await OperateGuideApi.createOperateGuide(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        name: undefined,
        type: undefined,
        module: undefined,
        picUrl: undefined,
        sort: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
